﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading;
using Arch.Data;
using Arch.Samples.Data.Contracts;
using Arch.Samples.Model;
using Microsoft.Practices.Unity;

namespace Arch.Samples.Data
{
    public class TodoRepository : Repository<Todo>, ITodoRepository
    {
        [Dependency]
        public IPrincipal Principal { get; set; }

        [Dependency]
        public IRepository<User> UserRepository { get; set; }

        public IEnumerable<Todo> Search(string query)
        {
            return Find(e =>
                        e.GlobalCategory.Name.ToLower().Contains(query.ToLower()) ||
                        e.SubCategory.Name.ToLower().Contains(query.ToLower()) ||
                        e.Description.ToLower().Contains(query.ToLower()) ||
                        e.Title.ToLower().Contains(query.ToLower())
                        );
        }

        public override Todo Create(Todo entity)
        {
            entity.CreationUser = UserRepository.GetOrCreate(u => u.Username == Principal.Identity.Name);
            return base.Create(entity);
        }
    }
}
